﻿using System;
using System.Collections.Generic;

namespace BusinessLayer
{
    /// <summary>
    /// The user.
    /// </summary>
    public class User : BusinessEntity
    {
        /// <summary>
        /// The user repository.
        /// </summary>
        public class UserRepository : Repository<User>
        {
            public bool ValidateUser(string username, string password)
            {
                Reader<User> reader = this.ReadAll();
                while (reader.Read())
                {
                    if (reader.Current.Username == username && reader.Current.Password == password)
                        return true;
                }
                reader.Close();
                return false;
            }
        }

        /// <summary>
        /// The user repository.
        /// </summary>
        public static UserRepository Repository = new UserRepository();

        /// <summary>
        /// Gets the id.
        /// </summary>
        public int Id
        {
            get { return (int)this.Get("Id"); }
        }

        /// <summary>
        /// Gets or sets the username.
        /// </summary>
        public string Username
        {
            get { return (string)this.Get("Username"); }
            set { this.Set("Username", value); }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        public string Password
        {
            get { return (string)this.Get("Password"); }
            set { this.Set("Password", value); }
        }

        /// <summary>
        /// Gets or sets the privilege level.
        /// </summary>
        public PrivilegeLevel PrivilegeLevel
        {
            get { return PrivilegeLevel.Repository.ReadById((int)this.Get("PrivilegeLevel")); }
            set { this.Set("PrivilegeLevel", value.Id); }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="User"/> class.
        /// </summary>
        internal User(Dictionary<string, object> user)
        {
            this.Initialize(user);
        }

        /// <summary>
        /// Default constructor.
        /// Must call Initialize method before using.
        /// </summary>
        public User()
        {
        }


        /// <summary>
        /// Save the user.
        /// </summary>
        public override void Save()
        {
            User.Repository.Write(this);
        }

    }
}
